package lex.netant;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.lex.swing.ContainerFactory;


public class NetAntMainFrame extends JFrame {
	private JMenu file, edit, download, view, option, tool, help;
	private JMenuItem exit, add, preference, bookmark;
	private static JMenuItem start, stop;
	private JMenuItem helptopic, about;
	private JCheckBoxMenuItem istoolbar, isstatusbar;
	private JMenuBar menubar;
	private JToolBar toolbar;
	private JButton addBut, helpBut;
	private static JButton startBut, stopBut;
	private JButton preferenceBut, exitBut;
	Listener listener;
	private Container contentPane;
	private JSplitPane mainSplit;
	JScrollPane scrollPane;//
	JPanel panel;
	// static JProgressBar pb;
	JLabel label, statusBar;
	JTree tree;

	public NetAntMainFrame(String name) {
		super(name);
		init();
	}

	/* ��ʼ��������� */
	private void init() {
		tree = new JTree(createNodes());
		tree.setRootVisible(true);
		// tree.setEditable(true);
		label = new JLabel("ProgressMonitor:");
		panel = new JPanel();
		panel.add(label);
		statusBar = new JLabel("StatusBar");
		mainSplit = ContainerFactory.createBorderlessSplitPane(5);
		mainSplit.setLeftComponent(new JScrollPane(tree));
		mainSplit.setRightComponent(panel);

		contentPane = getContentPane();
		listener = new Listener();
		toolbar = new JToolBar();
		// toolbar.setBackground(Color.green);
		// toolbar.setForeground(Color.blue);
		menubar = new JMenuBar();
		// menubar.setBackground(Color.green);
		// menubar.setForeground(Color.blue);
		file = new JMenu("File");
		file.setMnemonic('F');
		exit = new JMenuItem("Exit");
		exit.setMnemonic('X');
		exit.addActionListener(listener);
		exitBut = new JButton("Exit");
		exitBut.setToolTipText("Exit");
		exitBut.addActionListener(listener);
		file.add(exit);
		edit = new JMenu("Edit");
		add = new JMenuItem("Add...");
		add.addActionListener(listener);
		addBut = new JButton("Add...");
		addBut.setToolTipText("Add...");
		addBut.addActionListener(listener);
		edit.add(add);
		download = new JMenu("Download");
		start = new JMenuItem("Start");
		start.addActionListener(listener);
		start.setEnabled(false);
		startBut = new JButton("Start");//
		startBut.addActionListener(listener);
		startBut.setEnabled(false);
		startBut.setToolTipText("Start");
		stopBut = new JButton("Stop");//
		stopBut.addActionListener(listener);
		stopBut.setToolTipText("Stop");
		stopBut.setEnabled(false);
		stop = new JMenuItem("Stop");
		stop.addActionListener(listener);
		stop.setEnabled(false);
		download.add(start);
		download.add(stop);
		view = new JMenu("View");
		istoolbar = new JCheckBoxMenuItem("ToolBar");
		isstatusbar = new JCheckBoxMenuItem("StatusBar");
		view.add(istoolbar);
		view.add(isstatusbar);
		option = new JMenu("Option");
		preference = new JMenuItem("Preference...");
		preference.addActionListener(listener);
		preferenceBut = new JButton("Preference...");
		preferenceBut.setToolTipText("Preference...");
		preferenceBut.addActionListener(listener);
		option.add(preference);
		tool = new JMenu("Tool");
		bookmark = new JMenuItem("BookMark...");
		bookmark.addActionListener(listener);
		tool.add(bookmark);
		help = new JMenu("Help");
		helptopic = new JMenuItem("Help Topic...");
		helptopic.addActionListener(listener);
		helpBut = new JButton("Help");
		helpBut.setToolTipText("Help");
		helpBut.addActionListener(listener);
		about = new JMenuItem("About...");
		about.addActionListener(listener);
		help.add(helptopic);
		help.add(about);
		menubar.add(file);
		menubar.add(edit);
		menubar.add(download);
		menubar.add(view);
		menubar.add(option);
		menubar.add(tool);
		menubar.add(help);
		toolbar.add(startBut);
		toolbar.add(stopBut);
		toolbar.add(addBut);
		toolbar.add(preferenceBut);
		toolbar.addSeparator();
		toolbar.add(helpBut);
		toolbar.addSeparator();
		toolbar.add(exitBut);
		contentPane.add(toolbar, BorderLayout.NORTH);
		contentPane.add(mainSplit, BorderLayout.CENTER);
		contentPane.add(statusBar, BorderLayout.SOUTH);
		setJMenuBar(menubar);
	}

	private DefaultMutableTreeNode createNodes() {
		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Virtual Folder");
		DefaultMutableTreeNode missionState = new DefaultMutableTreeNode("Mission State");
		DefaultMutableTreeNode downDate = new DefaultMutableTreeNode("Down Date");
		DefaultMutableTreeNode today = new DefaultMutableTreeNode("Today");
		DefaultMutableTreeNode yesterday = new DefaultMutableTreeNode("Yesterday");
		DefaultMutableTreeNode lastWeek = new DefaultMutableTreeNode("Last Week");
		DefaultMutableTreeNode normal = new DefaultMutableTreeNode("Normal");
		DefaultMutableTreeNode running = new DefaultMutableTreeNode("Running");
		rootNode.add(missionState);
		rootNode.add(downDate);
		downDate.add(today);
		downDate.add(yesterday);
		downDate.add(lastWeek);
		missionState.add(normal);
		missionState.add(running);
		return rootNode;
	}

	/*
	 * public static JProgressBar getProgressBar(){ return pb; }
	 */
	public static void setStartTrue() {
		startBut.setEnabled(true);
		start.setEnabled(true);
	}

	public static void setStartFalse() {
		startBut.setEnabled(false);
		start.setEnabled(false);
	}

	public static void setStopTrue() {
		stopBut.setEnabled(true);
		stop.setEnabled(true);
	}

	public static void setStopFalse() {
		stopBut.setEnabled(false);
		stop.setEnabled(false);
	}

	public void showAddDialog() {
		AddDialog ad = new AddDialog(this);
		ad.setSize(380, 340);
		ad.setModal(true);
		ad.center();
		ad.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		ad.show();
	}

	public void showPrefference() {
		Prefference p = new Prefference(this);
		p.setSize(380, 340);
		p.setModal(true);
		p.center();
		p.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		p.show();
	}

	public void showAbout() {
		About about = new About(this);
		about.setSize(380, 340);
		about.setModal(true);
		about.center();
		about.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		/*
		 * about.addWindowListener(new WindowAdapter(){ public void
		 * windowClosed(WindowEvent e){ about.dispose(); } });
		 */
		about.show();
	}

	class Listener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String arg = e.getActionCommand();
			if (arg == "Exit") {
				System.exit(0);
			} else if (arg == "Add...") {
				showAddDialog();//
			} else if (arg == "Start") {
				Utility.stop = false;
				DownEnd de = new DownEnd();
				de.start();
				SplitFile split = new SplitFile();
				split.start();
			} else if (arg == "Stop") {
				stopBut.setEnabled(false);
				startBut.setEnabled(true);
				Utility.stop = true;
			} else if (arg == "Preference...") {
				showPrefference();
			} else if (arg == "BookMark...") {
			} else if (arg == "About...") {
				showAbout();
			}
		}
	}
}
